package com.ljh.ad.index.unitkeyword;

import com.ljh.ad.index.IndexAware;
import com.ljh.ad.untils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;

/**
 * @Author: ljh
 * @Date: 2020/3/2 21:41
 * 倒序索引 通过关键词找对应的推广单元id
 */
@Slf4j
@Component
public class UnitKeywordIndex implements IndexAware<String, Set<Long>> {

    /**
     * 通过关键词找id
     */
    private static Map<String, Set<Long>> keywordUnitMap;

    /**
     * 通过id找关键词
     */
    private static Map<Long, Set<String>> unitKeywordMap;

    static {
        keywordUnitMap = new ConcurrentHashMap<>();
        unitKeywordMap = new ConcurrentHashMap<>();
    }
    @Override
    public Set<Long> get(String key) {
        if (StringUtils.isEmpty(key)) {
            return Collections.emptySet();
        }

        Set<Long> result = keywordUnitMap.get(key);
        if (result == null) {
            return Collections.emptySet();
        }

        return result;
    }

    @Override
    public void add(String key, Set<Long> value) {

        log.info("UnitKeywordIndex, before add: {}", unitKeywordMap);
        //更新关键词到推广单元的索引，如果已有对应关系则直接添加，没有则添加新的set
        Set<Long> unitIds=CommonUtils.getOrCreate(key,keywordUnitMap, ConcurrentSkipListSet::new);
        unitIds.addAll(value);

        value.forEach(id->{
         Set<String> keyWords=CommonUtils.getOrCreate(id,unitKeywordMap,ConcurrentSkipListSet::new);
         keyWords.add(key);
        });
        log.info("UnitKeywordIndex, after add: {}", unitKeywordMap);
    }

    @Override
    public void update(String key, Set<Long> value) {

    }

    @Override
    public void delete(String key, Set<Long> value) {
        log.info("UnitKeywordIndex, before delete: {}", unitKeywordMap);

        Set<Long> unitIds = CommonUtils.getOrCreate(
                key, keywordUnitMap,
                ConcurrentSkipListSet::new
        );
        unitIds.removeAll(value);

        for (Long unitId : value) {

            Set<String> keywordSet = CommonUtils.getOrCreate(
                    unitId, unitKeywordMap,
                    ConcurrentSkipListSet::new
            );
            keywordSet.remove(key);
        }

        log.info("UnitKeywordIndex, after delete: {}", unitKeywordMap);
    }

    /**
     * 用来匹配关键词是否在此推广单元中
     * @param unitId
     * @param keywords
     * @return
     */
    public boolean match(Long unitId, List<String> keywords) {

        //id存在且对应的关键词集合不为空
        if (unitKeywordMap.containsKey(unitId)
                && CollectionUtils.isNotEmpty(unitKeywordMap.get(unitId))) {

            Set<String> unitKeywords = unitKeywordMap.get(unitId);

            //CollectionUtils.isSubCollection(A, B);
            //判断A是否是B的子集
            return CollectionUtils.isSubCollection(keywords, unitKeywords);
        }

        return false;
    }
}
